package com.example.administrator.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    String jsonStr = "[\n" +
            "    { \"id\":\"1\",\"name\":\"张三\",\"age\":\"18\" },\n" +
            "    { \"id\":\"2\",\"name\":\"李四\",\"age\":\"18\"  },\n" +
            "    { \"id\":\"3\",\"name\":\"王五\",\"age\":\"18\" }\n" +
            "]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    InputStream in = getResources().getAssets().open("data.txt");
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    StringBuffer buffer = new StringBuffer();
                    String line = bufferedReader.readLine();
                    while (line != null){
                        buffer.append(line);
                        line = bufferedReader.readLine();
                    }
                    Log.e(TAG, "省市区 : " + buffer.toString());

                    //获取广东省广州市所有区
                    JSONArray jsonArray = new JSONArray(buffer.toString());
                    for(int i=0; i<jsonArray.length(); i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String province = jsonObject.getString("name");
//                        Log.e(TAG, "province = " + province);

                        if(province.equals("广东省")){
                            JSONArray cityArray = jsonObject.getJSONArray("city");
                            for(int j=0; j<cityArray.length(); j++){
                                JSONObject cityObject = cityArray.getJSONObject(j);
                                String cityName = cityObject.getString("name");
//                                Log.e(TAG, "cityName = " + cityName);

                                if(cityName.equals("广州市")){
                                    JSONArray areaArray = cityObject.getJSONArray("area");
                                    for(int k=0; k<areaArray.length(); k++){
                                        String area = areaArray.getString(k);
                                        Log.e(TAG, "area = " + area);
                                    }
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        //解析Json
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONArray jsonArray = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String id = jsonObject.getString("id");
                        String name = jsonObject.getString("name");
                        String age = jsonObject.getString("age");
                        Log.e(TAG, "id = " + id + " name = " + name + " age = " + age);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        final Student s1 = new Student("小三", 18, "17001");
        final Student s2 = new Student("小四", 20, "16001");
        final Student s3 = new Student("小五", 22, "15001");
        final ArrayList<Student> studentArrayList = new ArrayList<>();
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        //组装Json对象
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name", s1.getName());
                    jsonObject.put("age", s1.getAge());
                    jsonObject.put("stuNo", s1.getStuNo());
                    Log.e(TAG, "jsonObject = " + jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        //组装Json数组
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONArray jsonArray = new JSONArray();
                    for (Student s : studentArrayList) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", s.getName());
                        jsonObject.put("age", s.getAge());
                        jsonObject.put("stuNo", s.getStuNo());

                        jsonArray.put(jsonObject);
                    }

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("stuList", jsonArray);
                    Log.e(TAG, "jsonObject = " + jsonObject.toString() );
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
